﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AppCommon;
using DevComponents.DotNetBar.SuperGrid;
using Microsoft.Synchronization;

namespace SyncConfig.Dialog
{
    public partial class ClientConfig : SyncConfig.Dialog.BaseDialog
    {
        public Guid ClientId { get; set; }
        public ClientConfig()
        {
            InitializeComponent();
        }

        private void ClientConfig_Load(object sender, EventArgs e)
        {
            LoadScopeConfig();
        }
        void LoadScopeConfig()
        {
            DataTable tblDirection = new DataTable();
            tblDirection.Columns.Add("Direction");
            tblDirection.Columns.Add("Name");
            tblDirection.Rows.Add("Undefine", "Chưa chọn");
            tblDirection.Rows.Add(SyncDirectionOrder.DownloadAndUpload.ToString(), "DownloadAndUpload");
            tblDirection.Rows.Add(SyncDirectionOrder.UploadAndDownload.ToString(), "UploadAndDownload");
            tblDirection.Rows.Add(SyncDirectionOrder.Download.ToString(), "DownloadOnly");
            tblDirection.Rows.Add(SyncDirectionOrder.Upload.ToString(), "UploadOnly");
            GridColumn col = gridClient.PrimaryGrid.Columns["Direction"];
            GridComboBoxExEditControl combox = (GridComboBoxExEditControl)col.RenderControl;
            DataView view1 = new DataView(tblDirection);
            combox.DisplayMember = "Name";
            combox.ValueMember = "Direction";
            combox.DataSource = view1;
            combox = (GridComboBoxExEditControl)col.EditControl;
            combox.DisplayMember = "Name";
            combox.ValueMember = "Direction";
            combox.DataSource = view1;

            col = gridClient.PrimaryGrid.Columns["ScopeName"];
            combox = (GridComboBoxExEditControl)col.RenderControl;
            DataView view2 = new DataView(FrmMain.MainForm.TreeManipulation.dsSyncConfig.ScopeInfoEx);
            combox.DisplayMember = "ScopeName";
            combox.ValueMember = "ScopeExId";
            combox.DataSource = view2;
            SyncConfigDS.ClientConfigDataTable tblConfig = FrmMain.MainForm.TreeManipulation.dsSyncConfig.ClientConfig;

            DataView view = new DataView(tblConfig);
            view.RowFilter = "ClientId='" + ClientId.ToString() + "'";
            view.Sort = "ScopeExId";
            foreach (SyncConfigDS.ScopeInfoExRow row in FrmMain.MainForm.TreeManipulation.dsSyncConfig.ScopeInfoEx)
            {
                if (view.Find(row.ScopeExId) < 0)
                {
                    SyncConfigDS.ClientConfigRow newConfig = tblConfig.NewClientConfigRow();
                    newConfig.ClientId = ClientId;
                    newConfig.ScopeExId = row.ScopeExId;
                    newConfig.ClientName = "";
                    newConfig.Des = "";
                    newConfig.AssemblyVersion = 1;
                    newConfig.ScopeVersion = 1;
                    newConfig.SchemaVersion = 1;
                    newConfig.LogLocation = "nothing";
                    newConfig.BatchSize = 0;
                    newConfig.Direction = "Undefine";
                    newConfig.DownloadFirst = true;
                    newConfig.UploadFirst = true;

                    
                    tblConfig.AddClientConfigRow(newConfig);
                }
            }
            gridClient.PrimaryGrid.DataSource = view;
         

           

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn lưu thông tin cấu hình?", "Xac nhan", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                DataView view = gridClient.PrimaryGrid.DataSource as DataView;
                if (view.Table.GetChanges() != null)
                {
                    foreach (DataRowView rv in view)
                    {
                        if (rv.Row.IsNull("SyncTimes"))
                            rv["SyncTimes"] = 0;

                        rv["ConfigTimes"] = rv["SyncTimes"];
                    }

                    FrmMain.MainForm.TreeManipulation.AdapterClientConfigTable.Update(view.Table as SyncConfigDS.ClientConfigDataTable);
                    view.Table.AcceptChanges();
                }
                Close();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            DataView view = gridClient.PrimaryGrid.DataSource as DataView;
            foreach (DataRowView rv in view)
                rv["Enable"] = checkBox1.Capture;
        }

        private void ckUploadAll_CheckedChanged(object sender, EventArgs e)
        {
            DataView view = gridClient.PrimaryGrid.DataSource as DataView;
            foreach (DataRowView rv in view)
                rv["UploadFirst"] = ckUploadAll.Checked;
        }

        private void ckDownloadAll_CheckedChanged(object sender, EventArgs e)
        {
            DataView view = gridClient.PrimaryGrid.DataSource as DataView;
            foreach (DataRowView rv in view)
                rv["DownloadFirst"] = ckDownloadAll.Checked;
        }

    }
}
